package com.example.demo.controller;

import com.example.demo.entity.Message;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @PostMapping("/send")
    public void sendMessage(@RequestBody Message message) {
        messageService.sendMessage(message.getSender(), message.getReceiver(), message.getContent());
    }

    @GetMapping("/{user_id}/{targetUser_id}")
    public List<Message> getMessages(@PathVariable Integer user_id, @PathVariable Integer targetUser_id) {
        return messageService.getMessages(user_id, targetUser_id);
    }

    @GetMapping("/conversation/{user_id}/{targetUser_id}")
    public List<Message> getConversation(@PathVariable Integer user_id, @PathVariable Integer targetUser_id) {
        return messageService.getConversation(user_id, targetUser_id);
    }

}